package com.hecore.lagou.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @Author: hecore
 * @Date: 2020/10/12 18:53
 * @Description: IO 流操作
 */

public class HdfsIOSample {

    public static String baseHost="hdfs://192.168.56.101:9000";


    @Test
    public void readFileSeek2() throws IOException, InterruptedException,
            URISyntaxException{
        // 1 获取⽂件系统
        Configuration configuration = new Configuration();
        FileSystem fs = getBaseFileSystem(configuration);
        // 2 打开输⼊流,读取数据输出到控制台
        FSDataInputStream in = null;
        try{
            in= fs.open(new Path("/lagou.txt"));
            IOUtils.copyBytes(in, System.out, 4096, false);
            in.seek(0); //从头再次读取
            IOUtils.copyBytes(in, System.out, 4096, false);
        }finally {
            IOUtils.closeStream(in);
            fs.close();
        }

    }

    @Test
    public void upload() throws IOException, InterruptedException,
            URISyntaxException {
        Configuration configuration = new Configuration();
        // 1 获取⽂件系统
        FileSystem fs = getBaseFileSystem(configuration);
        // 2 创建输⼊流
        FileInputStream fis = new FileInputStream(new File("e:/lagou.txt"));
        // 3 获取输出流
        FSDataOutputStream fos = fs.create(new Path("/lagou_io.txt"));
        // 4 流对拷
        IOUtils.copyBytes(fis, fos, configuration);
        // 5 关闭资源
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fs.close();

    }

    // ⽂件下载

    @Test
    public void down() throws IOException, InterruptedException,
            URISyntaxException{
        // 1 获取⽂件系统
        Configuration configuration = new Configuration();
        FileSystem fs = getBaseFileSystem(configuration);

        // 2 获取输⼊流
        FSDataInputStream fis = fs.open(new Path("/lagou_io.txt"));

        // 3 获取输出流
        FileOutputStream fos = new FileOutputStream(new
                File("e:/lagou_io_copy.txt"));

        // 4 流的对拷
        IOUtils.copyBytes(fis, fos, configuration);

        // 5 关闭资源
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fs.close();
    }

    public FileSystem getBaseFileSystem(Configuration configuration){
        FileSystem fs = null;
        try {
            fs = FileSystem.get(new URI(baseHost),
                    configuration, "root");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return fs;
    }


}
